package com.ruoyi.project.system.farmer.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.base_family_info.domain.BaseFamilyInfo;
import com.ruoyi.project.system.base_family_info.service.IBaseFamilyInfoService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.farmer.domain.Farmer;
import com.ruoyi.project.system.farmer.service.IFarmerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 农户管理Controller
 *
 * @author ssk
 * @date 2023-02-16
 */
@Controller
@RequiresRoles(value={"common", "admin"},logical= Logical.OR)
@RequestMapping("/system/farmer")
@Service
public class FarmerController extends BaseController
{
    private String prefix = "system/farmer";

    @Autowired
    private IFarmerService farmerService;
    @Autowired
    private IBaseFamilyInfoService baseFamilyInfoService;

    @RequiresPermissions("system:farmer:view")
    @GetMapping()
    public String farmer()
    {
        return prefix + "/farmer";
    }

    /**
     * 查询农户管理列表
     */
    @RequiresPermissions("system:farmer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Farmer farmer)
    {
        startPage();
        List<Farmer> list = farmerService.selectFarmerList(farmer);
        return getDataTable(list);
    }

    /**
     * 导出农户管理列表
     */
    @RequiresPermissions("system:farmer:export")
    @Log(title = "农户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Farmer farmer)
    {
        List<Farmer> list = farmerService.selectFarmerList(farmer);
        ExcelUtil<Farmer> util = new ExcelUtil<Farmer>(Farmer.class);
        return util.exportExcel(list, "farmer");
    }

    /**
     * 新增农户管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存农户管理
     */
    @RequiresPermissions("system:farmer:add")
    @Log(title = "农户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody Farmer farmer) throws ParseException {
        return toAjax(farmerService.insertFarmer(farmer));
    }

    /**
     * 修改农户管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Farmer farmer = farmerService.selectFarmerById(id);
        mmap.put("farmer", farmer);
        return prefix + "/edit";
    }

    /**
     * 跳转至转移户主页面
     */
    @GetMapping("/transfer/{id}")
    public String transfer(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Farmer farmer = farmerService.selectFarmerById(id);
        String fid = farmer.getFid();
        BaseFamilyInfo baseFamilyInfo = baseFamilyInfoService.selectBaseFamilyInfoById(fid);
        mmap.put("baseFamilyInfo", baseFamilyInfo);
        List<Farmer> list = farmerService.selectMember(fid);
        mmap.put("member",list);
        mmap.put("size",list.size());
        return prefix + "/transfer";
    }

    /**
     * 修改保存农户管理
     */
    @RequiresPermissions("system:farmer:edit")
    @Log(title = "农户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody Farmer farmer) throws ParseException {
        return toAjax(farmerService.updateFarmer(farmer));
    }

    /**
     * 删除农户管理
     */
    @RequiresPermissions("system:farmer:remove")
    @Log(title = "农户管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(farmerService.deleteFarmerByIds(ids));
    }

    /**
     * 注销非户主或仅剩户主一人
     */
    @RequiresPermissions("system:farmer:logout")
    @PostMapping("/logout")
    @ResponseBody
    public AjaxResult logout(String ids)
    {
        int id = Integer.parseInt(ids);
        return toAjax(farmerService.logoutFarmerById(id));
    }

    /**
     * 修改保存转移户主
     */
    @RequiresPermissions("system:farmer:transfer")
    @Log(title = "农户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/transferSave")
    @ResponseBody
    public AjaxResult transferSave(String fid,String oldName, String newName,String str)
    {
        return toAjax(farmerService.transferSave(fid,oldName,newName,str));
    }

    /**
     * 查询户人口数与关系
     */
    @RequiresPermissions("system:farmer:selectNumber")
    @PostMapping("/selectnumship")
    @ResponseBody
    public HashMap selectNumber(String ids)
    {
        int id = Integer.parseInt(ids);
        return farmerService.selectNumShip(id);
    }

    /**
     * 校验身份证号
     */
    @PostMapping("/checkIdCardUnique")
    @ResponseBody
    public int checkPhoneUnique(String idCard)
    {
        return farmerService.checkIdCardUnique(idCard);
    }


    /**
     * 查询农户个人信息
     */
    @PostMapping("/getFarmerByName")
    @ResponseBody
    public Farmer getFarmerByName(String Name) {
        return farmerService.getFarmerByName(Name);
    }
}
